﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YJKArchMethodLibrary.Utils;

namespace Aproll.Forms
{
	public partial class CreateAprollForm : System.Windows.Forms.Form
	{
		public CreateAprollForm.FormResultStyle ResultStyle
		{
			get
			{
				return this.m_FormResultStyle;
			}
		}

		public double AprollWidth
		{
			get
			{
				return Common.MMToFeet(Convert.ToDouble(this.m_AprollWidth));
			}
		}

		public double AprollAngle
		{
			get
			{
				return Convert.ToDouble(this.m_AprollAngle);
			}
		}

		public double AprollMinHeight
		{
			get
			{
				return Common.MMToFeet(Convert.ToDouble(this.m_AprollMinHeight));
			}
		}

		public double Offset
		{
			get
			{
				return Common.MMToFeet(Convert.ToDouble(this.m_Offset));
			}
		}

		public double LevelOffset
		{
			get
			{
				return Common.MMToFeet(Convert.ToDouble(this.m_LevelOffset));
			}
		}

		public CreateAprollForm()
		{
			this.InitializeComponent();
			this.toolTip1 = new ToolTip();
			this.toolTip2 = new ToolTip();
			this.toolTip3 = new ToolTip();
			this.toolTip4 = new ToolTip();
			this.toolTip5 = new ToolTip();
		}

		private void CreateAprollForm_Load(object sender, EventArgs e)
		{
			this.ReadData();
			this.pictureBox.Image = this.imageList.Images[5];
		}

		private void CreateAprollForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.ResultStyle == CreateAprollForm.FormResultStyle.FRS_Search && ((Form)sender).DialogResult == DialogResult.Cancel)
			{
				this.m_FormResultStyle = CreateAprollForm.FormResultStyle.FRS_OK;
				e.Cancel = true;
			}
			if (this.ResultStyle == CreateAprollForm.FormResultStyle.FRS_Edit && ((Form)sender).DialogResult == DialogResult.Cancel)
			{
				this.m_FormResultStyle = CreateAprollForm.FormResultStyle.FRS_OK;
				e.Cancel = true;
			}
			if (((Form)sender).DialogResult != DialogResult.OK)
			{
				return;
			}
			if (this.m_FormResultStyle != CreateAprollForm.FormResultStyle.FRS_OK)
			{
				this.SaveData();
				return;
			}
			if (!this.DataIsCorrect())
			{
				e.Cancel = true;
				return;
			}
			this.SaveData();
		}

		private void ReadData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "CreateAprollForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.m_AprollWidth = "100";
				this.tbAprollWidth.Text = this.m_AprollWidth;
				this.m_AprollAngle = "3";
				this.tbAprollAngle.Text = this.m_AprollAngle;
				this.m_AprollMinHeight = "30";
				this.tbAprollMinHeight.Text = this.m_AprollMinHeight;
				this.m_Offset = "0";
				this.tbOffset.Text = this.m_Offset;
				this.m_LevelOffset = "0";
				this.tbLevelOffset.Text = this.m_LevelOffset;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			int num = Convert.ToInt32(streamReader.ReadLine());
			if (num >= 0)
			{
				this.m_AprollWidth = streamReader.ReadLine();
				this.tbAprollWidth.Text = this.m_AprollWidth;
				this.m_AprollAngle = streamReader.ReadLine();
				this.tbAprollAngle.Text = this.m_AprollAngle;
				this.m_AprollMinHeight = streamReader.ReadLine();
				this.tbAprollMinHeight.Text = this.m_AprollMinHeight;
				this.m_Offset = "0";
				this.tbOffset.Text = this.m_Offset;
			}
			if (num > 1)
			{
				this.m_Offset = streamReader.ReadLine();
				this.tbOffset.Text = this.m_Offset;
				Convert.ToBoolean(streamReader.ReadLine());
			}
			if (num > 2)
			{
				this.m_LevelOffset = streamReader.ReadLine();
				this.tbLevelOffset.Text = this.m_LevelOffset;
			}
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "CreateAprollForm.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(CreateAprollForm.m_Version);
			streamWriter.WriteLine(this.m_AprollWidth);
			streamWriter.WriteLine(this.m_AprollAngle);
			streamWriter.WriteLine(this.m_AprollMinHeight);
			streamWriter.WriteLine(this.m_Offset);
			bool value = true;
			streamWriter.WriteLine(value);
			streamWriter.WriteLine(this.m_LevelOffset);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool DataIsCorrect()
		{
			if (!Common.IsValidating(this.tbAprollWidth.Text, 100.0, 1500.0))
			{
				MessageBox.Show("\"散水宽度\"的合法范围在100mm～1500mm之间", "盈建科提示");
				this.m_AprollWidth = "100";
				this.tbAprollWidth.Text = this.m_AprollWidth;
				return false;
			}
			if (!Common.IsValidating(this.tbAprollAngle.Text, 3.0, 5.0))
			{
				MessageBox.Show("\"散水坡度\"的合法范围在3%～5%之间", "盈建科提示");
				this.m_AprollAngle = "3";
				this.tbAprollAngle.Text = this.m_AprollAngle;
				return false;
			}
			if (!Common.IsValidating(this.tbAprollMinHeight.Text, 30.0, 500.0))
			{
				MessageBox.Show("\"散水外缘高度\"的合法范围在30mm～500mm之间", "盈建科提示");
				this.m_AprollMinHeight = "30";
				this.tbAprollMinHeight.Text = this.m_AprollMinHeight;
				return false;
			}
			if (!Common.IsValidating(this.tbOffset.Text, 0.0, 2000.0))
			{
				MessageBox.Show("\"距墙距离\"的合法范围在0mm～2000mm之间", "盈建科提示");
				this.m_Offset = "0";
				this.tbOffset.Text = this.m_Offset;
				return false;
			}
			if (!Common.IsValidating(this.tbLevelOffset.Text, -5000.0, 5000.0))
			{
				MessageBox.Show("\"标高偏移\"的合法范围在-5000mm～5000mm之间", "盈建科提示");
				this.m_LevelOffset = "0";
				this.tbLevelOffset.Text = this.m_LevelOffset;
				return false;
			}
			return true;
		}

		private void tbAprollWidth_TextChanged(object sender, EventArgs e)
		{
			if (!Common.IsValidating(this.tbAprollWidth.Text, 0.0, 1500.0))
			{
				this.tbAprollWidth.Text = this.m_AprollWidth;
				return;
			}
			this.m_AprollWidth = this.tbAprollWidth.Text;
		}

		private void tbAprollHeight_TextChanged(object sender, EventArgs e)
		{
			if (!Common.IsValidating(this.tbAprollAngle.Text, 0.0, 5.0))
			{
				this.tbAprollAngle.Text = this.m_AprollAngle;
				return;
			}
			this.m_AprollAngle = this.tbAprollAngle.Text;
		}

		private void tbAprollMinHeight_TextChanged(object sender, EventArgs e)
		{
			if (!Common.IsValidating(this.tbAprollMinHeight.Text, 0.0, 500.0))
			{
				this.tbAprollMinHeight.Text = this.m_AprollMinHeight;
				return;
			}
			this.m_AprollMinHeight = this.tbAprollMinHeight.Text;
		}

		private void tbOffset_TextChanged(object sender, EventArgs e)
		{
			if (!Common.IsValidating(this.tbOffset.Text, 0.0, 2000.0))
			{
				this.tbOffset.Text = this.m_Offset;
				return;
			}
			this.m_Offset = this.tbOffset.Text;
		}

		private void tbLevelOffset_TextChanged(object sender, EventArgs e)
		{
			if (!Common.IsValidating(this.tbLevelOffset.Text, -5000.0, 5000.0) && this.tbLevelOffset.Text.CompareTo("-") != 0)
			{
				this.tbLevelOffset.Text = this.m_LevelOffset;
				return;
			}
			this.m_LevelOffset = this.tbLevelOffset.Text;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SetFocusToRevit()
		{
			int wMsg = 7;
			CreateAprollForm.SendMessage(AprollDesigner._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private void tbAprollWidth_MouseLeave(object sender, EventArgs e)
		{
			this.toolTip1.Hide((IWin32Window)sender);
			this.pictureBox.Image = this.imageList.Images[5];
			this.DataIsCorrect();
		}

		private void tbAprollAngle_MouseLeave(object sender, EventArgs e)
		{
			this.toolTip2.Hide((IWin32Window)sender);
			this.pictureBox.Image = this.imageList.Images[5];
			this.DataIsCorrect();
		}

		private void tbAprollMinHeight_MouseLeave(object sender, EventArgs e)
		{
			this.toolTip3.Hide((IWin32Window)sender);
			this.pictureBox.Image = this.imageList.Images[5];
			this.DataIsCorrect();
		}

		private void tbOffset_MouseLeave(object sender, EventArgs e)
		{
			this.toolTip4.Hide((IWin32Window)sender);
			this.pictureBox.Image = this.imageList.Images[5];
			this.DataIsCorrect();
		}

		private void tbLevelOffset_MouseLeave(object sender, EventArgs e)
		{
			this.toolTip5.Hide((IWin32Window)sender);
			this.pictureBox.Image = this.imageList.Images[5];
			this.DataIsCorrect();
		}

		private void tbAprollWidth_MouseEnter(object sender, EventArgs e)
		{
			this.toolTip1.Show("\"散水宽度\"的合法范围在100mm～1500mm之间", (IWin32Window)sender);
			this.pictureBox.Image = this.imageList.Images[0];
		}

		private void tbAprollAngle_MouseEnter(object sender, EventArgs e)
		{
			this.toolTip2.Show("\"散水坡度\"的合法范围在3%～5%之间", (IWin32Window)sender);
			this.pictureBox.Image = this.imageList.Images[1];
		}

		private void tbAprollMinHeight_MouseEnter(object sender, EventArgs e)
		{
			this.toolTip3.Show("\"散水外缘高度\"的合法范围在30mm～500mm之间", (IWin32Window)sender);
			this.pictureBox.Image = this.imageList.Images[2];
		}

		private void tbOffset_MouseEnter(object sender, EventArgs e)
		{
			this.toolTip4.Show("\"距墙距离\"的合法范围在0mm～2000mm之间", (IWin32Window)sender);
			this.pictureBox.Image = this.imageList.Images[3];
		}

		private void tbLevelOffset_MouseEnter(object sender, EventArgs e)
		{
			this.toolTip5.Show("\"标高偏移\"的合法范围在-5000mm～5000mm之间", (IWin32Window)sender);
			this.pictureBox.Image = this.imageList.Images[4];
		}

		private void btnSearch_Click(object sender, EventArgs e)
		{
			this.m_FormResultStyle = CreateAprollForm.FormResultStyle.FRS_Search;
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			this.m_FormResultStyle = CreateAprollForm.FormResultStyle.FRS_Edit;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.m_FormResultStyle = CreateAprollForm.FormResultStyle.FRS_OK;
		}

		private static int m_Version = 3;

		private string m_AprollWidth = "";

		private string m_AprollAngle = "";

		private string m_AprollMinHeight = "";

		private string m_Offset = "";

		private string m_LevelOffset = "";

		private ToolTip toolTip1;

		private ToolTip toolTip2;

		private ToolTip toolTip3;

		private ToolTip toolTip4;

		private ToolTip toolTip5;

		private CreateAprollForm.FormResultStyle m_FormResultStyle = CreateAprollForm.FormResultStyle.FRS_OK;

		public enum FormResultStyle
		{
			FRS_OK = 1,
			FRS_Search,
			FRS_Edit
		}
	}
}
